﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddSubCommittee : System.Web.UI.Page
    {
       
        MemberAL memberAL = new MemberAL();
    

        public Guid SelectedCommittee
        {
            get
            {
                if (Session["CommitteeId"] == null)
                {
                    return memberAL.GetAllCommittees()[0].CommitteeId;
                }
                else
                {
                    return new Guid(Session["CommitteeId"].ToString());
                }
            }
            set
            {
                Session["CommitteeId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<CommitteeDTO> committeeDTO = memberAL.GetAllCommittees();
            ddlSubCommitteeMainCommId.DataSource = committeeDTO;
            ddlSubCommitteeMainCommId.DataTextField = "Title";
            ddlSubCommitteeMainCommId.DataValueField = "CommitteeId";
            ddlSubCommitteeMainCommId.SelectedValue = SelectedCommittee.ToString();
            ddlSubCommitteeMainCommId.DataBind();


            
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubCommitteeSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                MemberAL memberAL = new MemberAL();
                List<CommitteeDTO> committee = memberAL.GetCommitteeById(new Guid(ddlSubCommitteeMainCommId.SelectedValue)).ToList();
                string subCommitteeName = "The " + ddlSubCommitteeType.SelectedValue + "subcommittee of " + committee.SingleOrDefault().Title;
                SubCommitteeDTO subCommiteeDTO = new SubCommitteeDTO
                {
                    MainCommitteeId = new Guid(ddlSubCommitteeMainCommId.SelectedValue),
                    SubCommitteeId = Guid.NewGuid(),
                    SubCommitteeName = subCommitteeName,
                    Responsibility = txtSubCommitteeResponsibility.Text,
                    Description = txtSubCommDesc.Text,
                    SubCommiteePhoto = fuSubCommiteePhoto.FileName,
                    //ModifiedBy="Tharsan",
                    ModifiedTime = DateTime.Now

                };
              string result=  memberAL.saveSubCommittee(subCommiteeDTO);
              if (result == "Saved")
              {
                  lblAddSubCommMsg.Text = "The Sub Committee Details added Successfully";
                  txtSubCommitteeResponsibility.Text = "";
                  txtSubCommDesc.Text = "";
                  

              }
              else if (result == "Updated")
              {
                  lblAddSubCommMsg.Text = "The Sub Committee Details Updated Successfully";
                  txtSubCommitteeResponsibility.Text = "";
                  txtSubCommDesc.Text = "";
              }
              else if (result == "AlreadyExists")
              {
                  lblAddSubCommMsg.Text = "The Sub Committee Name is already exists for this Ex Committee";
                  txtSubCommitteeResponsibility.Text = "";
                  txtSubCommDesc.Text = "";
              }
              else
              {
                  lblAddSubCommMsg.Text = "Your data cannot be added";
                  txtSubCommitteeResponsibility.Text = "";
                  txtSubCommDesc.Text = "";
              }

            }
            catch(Exception ee)
            {
                lblAddSubCommMsg.Text = "There is an exception occured while inserting the Sub Committee details " + ee.ToString();
            }

        }
    }
}
